package com.fesc.trade.demo.service;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.httpclient.NameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fesc.trade.demo.constant.Constants;
import com.fesc.trade.demo.request.MerchantCreateRequest;
import com.fesc.trade.demo.request.MerchantQueryRequest;
import com.fesc.trade.demo.util.DateUtil;
import com.fesc.trade.demo.util.HttpUtil;
import com.fesc.trade.demo.util.SecurityUtil;

/**
 * 
 * 
 * @author prd-wfc
 * @version $Id: MerchantServiceImpl.java, v 0.1 2016年8月18日 下午4:47:01 prd-wfc Exp $
 */
public class MerchantServiceImpl implements MerchantService {

    /** 日志 */
    private static final Logger logger = LoggerFactory.getLogger(MerchantServiceImpl.class);

    @Override
    public String create() {

        /*******************公共参数********************/

        // (必填)收单机构编号
        String acquirerId = "10880010001";

        // (必填)签名类型 
        String signType = "RSA";

        // (必填)签名
        String sign = "";//根据参数生成

        // (必填)发送请求时间
        String timestamp = DateUtil.getNewFormatDateString(new Date());

        // 应用授权令牌, 第三方应用授权时填写，根据真实值填写
        String appAuthToken = "";

        // (必填)付款渠道
        String walletType = "alipay";

        /*******************业务参数********************/

        // (必填)商户外部编号，一个受理机构下面唯一
        String externalId = "1088001000114707129";

        // (必填)商户名称
        String name = "测试商户001";

        // (必填)商户简称
        String aliasName = "测试别名001";

        // (必填)客服电话
        String servicePhone = "95511";

        // (可选)联系人名称
        String contactName = "kris";

        // (可选)联系人电话
        String contactPhone = "0795-85022011";

        // (可选)联系人手机号
        String contactMobile = "13665302715";

        // (可选)联系人邮箱
        String contactEmail = "user@qq.com";

        // (必填)经营类目id
        String categoryId = "2015110500080520";

        // (可选)商户备注
        String memo = "新备注信息";

        // (必填)商户来源标识
        String source = "2088811361230225";

        // (可选)扩展信息
        String externalInfo = "扩展信息";

        /**************************封装参数******************************/
        MerchantCreateRequest merchantCreateRequest = new MerchantCreateRequest();

        merchantCreateRequest.setAcquirerId(acquirerId);
        merchantCreateRequest.setSignType(signType);
        merchantCreateRequest.setTimestamp(timestamp);
        merchantCreateRequest.setAppAuthToken(appAuthToken);
        merchantCreateRequest.setWalletType(walletType);

        merchantCreateRequest.setExternalId(externalId);
        merchantCreateRequest.setName(name);
        merchantCreateRequest.setAliasName(aliasName);
        merchantCreateRequest.setServicePhone(servicePhone);
        merchantCreateRequest.setContactName(contactName);
        merchantCreateRequest.setContactPhone(contactPhone);
        merchantCreateRequest.setContactMobile(contactMobile);
        merchantCreateRequest.setContactEmail(contactEmail);
        merchantCreateRequest.setCategoryId(categoryId);
        merchantCreateRequest.setMemo(memo);
        merchantCreateRequest.setSource(source);
        merchantCreateRequest.setExternalInfo(externalInfo);

        //生成签名
        Map<String, String> merchantCreateRequestParaMap = merchantCreateRequest.buildParaMap();

        String rsaSignStr;
        try {
            rsaSignStr = SecurityUtil.rsaSign(merchantCreateRequestParaMap, Constants.ENCODE_PRIVATE_KEY, StandardCharsets.UTF_8.displayName());
        } catch (Exception e) {
            throw new RuntimeException("加签失败", e);
        }

        merchantCreateRequestParaMap.put("sign", rsaSignStr);

        //
        merchantCreateRequest.setSign(rsaSignStr);
        merchantCreateRequest.validate();

        List<NameValuePair> paraList = buildPostParaList(merchantCreateRequestParaMap);

        String response = HttpUtil.httpClientPost(Constants.TEST_MERCHANT_CREATE_URL, paraList);

        return response;
    }

    @Override
    public String query() {

        /*******************公共参数********************/

        // (必填)收单机构编号
        String acquirerId = "10880010001";

        // (必填)签名类型 
        String signType = "RSA";

        // (必填)签名
        String sign = "";//根据参数生成

        // (必填)发送请求时间
        String timestamp = DateUtil.getNewFormatDateString(new Date());

        // 应用授权令牌, 第三方应用授权时填写，根据真实值填写
        String appAuthToken = "";

        // (必填)付款渠道
        String walletType = "alipay";

        /*******************业务参数********************/

        // (特殊可选)商户外部编号，一个受理机构下面唯一
        String externalId = "1088001000114707123";

        // (特殊可选)商户标识号
        String merchantId = "";

        // 扩展信息
        String externalInfo = "扩展信息";

        /**************************封装参数******************************/
        MerchantQueryRequest merchantQueryRequest = new MerchantQueryRequest();

        merchantQueryRequest.setAcquirerId(acquirerId);
        merchantQueryRequest.setSignType(signType);
        merchantQueryRequest.setTimestamp(timestamp);
        merchantQueryRequest.setAppAuthToken(appAuthToken);
        merchantQueryRequest.setWalletType(walletType);

        merchantQueryRequest.setExternalId(externalId);
        merchantQueryRequest.setMerchantId(merchantId);
        merchantQueryRequest.setExternalInfo(externalInfo);

        //生成签名
        Map<String, String> merchantQueryRequestParaMap = merchantQueryRequest.buildParaMap();

        String rsaSignStr;
        try {
            rsaSignStr = SecurityUtil.rsaSign(merchantQueryRequestParaMap, Constants.ENCODE_PRIVATE_KEY, StandardCharsets.UTF_8.displayName());
        } catch (Exception e) {
            throw new RuntimeException("加签失败", e);
        }

        merchantQueryRequestParaMap.put("sign", rsaSignStr);

        List<NameValuePair> paraList = buildPostParaList(merchantQueryRequestParaMap);

        //参数验证
        merchantQueryRequest.setSign(rsaSignStr);
        merchantQueryRequest.validate();

        String response = HttpUtil.httpClientPost(Constants.TEST_MERCHANT_QUERY_URL, paraList);

        return response;

    }

    public List<NameValuePair> buildPostParaList(Map<String, String> paraMap) {

        List<NameValuePair> pairList = new ArrayList<NameValuePair>(paraMap.size());

        for (String key : paraMap.keySet()) {
            NameValuePair nvPair = new NameValuePair(key, paraMap.get(key));
            pairList.add(nvPair);
        }

        return pairList;
    }

}
